package com.godme.string;

public class BoyerMoore {

    private static final int SIZE = 256;

    private static boolean[] characterFlag(String matchString){
        boolean[] arr = new boolean[SIZE];

        for(char c: matchString.toCharArray()){
            arr[c] = true;
        }
        return arr;
    }


    public static int find(String mainString, String matchString){
        int matchLength = matchString.length();
        int maxLength = mainString.length() - matchLength;
        boolean[] contains = characterFlag(matchString);
        int index = 0;
        int cursor;
        char tempCharacter;
        indexOffset: while (index <= maxLength){
            cursor = 0;
            for(;cursor<matchLength; cursor++){
                tempCharacter = mainString.charAt(index + cursor);
                if(tempCharacter != matchString.charAt(cursor)){
                    if(!contains[tempCharacter]){
                        index += cursor + 1;
                    }
                    continue indexOffset;
                }
            }
            return index;
        }
        return  -1;
    }

    public static void main(String[] args) {
        System.err.println(find("aaabc", "bc"));
    }


}
